package com.tx;

import java.beans.PropertyVetoException;


import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@EnableTransactionManagement
@Configuration
@ComponentScan("com.tx")
public class TxConfig {
	@Bean
	public PlatformTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}

	@Bean
	public DataSource dataSource() {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		try {
			dataSource.setDriverClass("com.mysql.jdbc.Driver.class");
			dataSource.setUser("root");
			dataSource.setPassword("123456");
			dataSource.setJdbcUrl("jdbc:mysql:///my_test");
		} catch (PropertyVetoException e) {
			System.out.println("错误！。。。。");
			e.printStackTrace();
		}
		return dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		// 直接使用几经注册好的Bean：调用方法名就好。
		return new JdbcTemplate(dataSource());
	}

}
